function foo(num) {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve(num * 2)
      }, 1000)
    })
  }
  
  
  
  // async
  function generatorToAsync(generatorFn) {
    return function() {
      const gen = generatorFn.apply(this, arguments)
  
      return new Promise((resolve, reject) => {
  
        // const g = generatorFn()
        // const next1 = g.next()
        // next1.value.then(res1 => {
  
        //   const next2 = g.next(res1)
        //   next2.value.then(res2 => {
  
        //     const next3 = g.next(res2)
        //     next3.value.then(res3 => {
  
        //       resolve(g.next(res3).value) // { value: 8, done: true }
  
        //     })
  
        //   })
  
        // })
  
        function loop(key, arg) {
          let res = null
  
          res = gen[key](arg) // gen.next(8)
  
          const { value, done } = res
          if (done) {
            return resolve(value)
          } else {
            // Promise.resolve(value) 为了保证 value 中的promise状态已经变更成 成功状态
            Promise.resolve(value).then(val => loop('next', val))
          }
  
        }
  
        loop('next')
      })
    }
  }
  
  function* gen() {
    const num1 = yield foo(1)
    const num2 = yield foo(num1)
    const num3 = yield foo(num2)
    return num3
  }
  const asyncFn = generatorToAsync(gen)
  // console.log(asyncFn()); // Promise{}
  asyncFn().then(res => {
    console.log(res);
  })
  
  
  
  
  // ------------------
  
// async function asyncFn2() {
//   const num1 = await foo(1)
//   const num2 = await foo(num1)
//   const num3 = await foo(num2)
//   return num3
// }
// asyncFn2().then(res => console.log(res))